Skip to content

Various Fixes for CPUID & MSR Adjustment (CPU Profiles) - #233

Closed
phip1611 wants to merge 19 commits into
gardenlinuxfrom
grdnlnx-cpu-profile-fixes
Closed

phip1611 wants to merge 19 commits into
gardenlinuxfrom
grdnlnx-cpu-profile-fixes

Conversation

@phip1611

@phip1611 phip1611 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

I asked Claude with the Opus 5.5. model to check everything regarding CPUID, MSRs, and the adjustments for CPU profiles. Seems to be not critical but I think we should at least take a look.

Pipeline: https://gitlab.cyberus-technology.de/cyberus/cloud/libvirt/-/merge_requests/332/pipelines

What changes

Runtime, for all profile VMs:

  • Snapshots and live migrations now save every MSR the host's KVM lists. Before, only the MSRs known when the profile was generated were saved, so guests silently lost IA32_DEBUGCTL (bus-lock detection) on hosts with Linux 7.1 or newer and MISC_FEATURES_ENABLES (CPUID faulting) everywhere.
  • The Cascade Lake profile is accepted on newer hosts booted with tsx=off.
  • IA32_ARCH_CAPABILITIES bit 63 is treated as reserved.

CPUID compatibility check (also for VMs without a profile):

  • The AVX10 version is checked.
  • The x87 defeature bits are ignored.

New profiles sapphire-rapids-v2 and cascade-lake-v2. The existing profiles stay byte-identical, because changing them would block migrating running VMs. Compared to v1, v2:

  • takes the VMX bit from the host, so it works with kvm_intel nested=0,
  • hides PKRU state, as PKU is hidden,
  • always sets the x87 defeature bits,
  • hides the WAITPKG VMX control from nested L1 hypervisors,
  • takes VMCS_ENUM from the running host,
  • fixes small SDM deviations without guest effect under KVM.

Tooling: the definition tables are corrected against the SDM, a compile-time check rejects overlapping or duplicate definitions, and the generator writes the final newline.

Confidence

  • rustfmt, clippy, and unit tests pass on every commit. Each new unit test fails without its fix.
  • An independent review checked every commit message claim against the SDM and the KVM source.
  • The v2 data was re-derived from the v1 data with the fixed definitions, as the original hosts were not available. The same method reproduces v1 exactly with the old definitions.
  • Raw CPUID leaf 7 on a Granite Rapids host has every bit that the profiles set.
  • Runtime tests: see the pipeline.

Existing VMs

  • VMs on sapphire-rapids or cascade-lake migrate to this version as before, because the v1 data is unchanged. From then on, all MSRs are migrated. State lost on earlier migrations returns only after a guest reboot.
  • They keep the v1 CPU model, including the nested=0 limitation. New VMs should use the -v2 profiles. Switching a VM needs a restart, and the guest then sees a slightly different CPU model.
  • Migrations to hosts with a lower AVX10 version are now refused. Bare-metal Xeon hosts are not expected to be affected.

The generator wrote the JSON without a final newline, which the
repository's .editorconfig requires. Profiles generated so far have
been fixed up by hand or not at all.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
TL;DR: CPU profiles no longer drop MSR state in snapshots.

With a CPU profile, snapshots and live migration only saved MSRs that
the profile's required list contains. That list is frozen when the
profile is generated, so MSRs that a newer KVM saves were silently
lost. One example is IA32_DEBUGCTL, which KVM saves since 7.1: guests
on the Sapphire Rapids profile lost bus-lock detection after a
migration. MISC_FEATURES_ENABLES (CPUID faulting) was lost on both
shipped profiles.

Save every MSR in the hypervisor's MSR index list, as the Host profile
does. The required list still has to be present on the host. VMX MSRs
stay out of the snapshot without nested virtualization.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
The Cascade Lake profile sets FB_CLEAR in IA32_ARCH_CAPABILITIES.
Newer hosts such as Sapphire Rapids do not, so the check only accepts
the profile if the host reports the MDS, TAA, and MMIO stale data "_NO"
bits. KVM clears TAA_NO when the host has no RTM, e.g. with tsx=off,
because TAA needs TSX. So the Cascade Lake profile was refused on
exactly the hosts that disable TSX, which the profiles expect.

Do not require TAA_NO when the hypervisor does not report RTM.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
The SDM lists bit 63 as reserved, but the compatibility check treated
it like a feature bit. A host that sets the bit was accepted for a
profile that does not, even though a future meaning of the bit could
be one that is unsafe when set. Require equality, as for the other
reserved bits.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
The AVX10 feature bit (CPUID.7.1:EDX[19]) is set for every AVX10
version, and the version itself in CPUID.24H:EBX[7:0] was not checked.
A CPU profile or a source VM with AVX10.2 was therefore accepted on a
host with AVX10.1 only, and the guest would fault on AVX10.2
instructions. Require the version to be not greater on the destination.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
@phip1611 phip1611 self-assigned this Nov 30, 2026
// that the hypervisor does not permit setting (e.g. various VMX CTLS, IA32_VMX_CR0_FIXED, IA32_VMX_CR4_FIXED).
// Snapshot every MSR the hypervisor lists, as the Host profile does.
// `required` is frozen when the profile is generated, so limiting the
// snapshot to it would silently drop MSRs that newer hypervisors save

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a desirable policy?

@phip1611 phip1611 changed the title arch: Fix CPU profile checks and add v2 profiles Various Fixes for CPUID & MSR Adjustment (CPU Profiles) Nov 30, 2026
@phip1611
phip1611 marked this pull request as ready for review November 30, 2026 15:09
CPUID.7.0:EBX[6] (FDP_EXCPTN_ONLY) and [13] (ZERO_FCS_FDS) are set when
an x87 feature is absent. They were checked like normal feature bits,
so a source with a bit set was refused on a destination without it,
while the opposite direction was accepted. Neither direction matters:
the hardware decides whether the x87 features are present, and KVM
documents that the guest CPUID value of these bits has no effect.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
The "Reserved at 111" field of CPUID.24H:EBX was defined at bits 7:0,
on top of the AVX10 version. The SDM places it at bits 18:16, which are
always 111b if AVX10 is supported. As no definition covered bits 18:16,
every profile generated on an AVX10 host such as Granite Rapids would
have zeroed them. Guests then see an AVX10 CPU that contradicts the
SDM. The shipped profiles have no AVX10 and do not change.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Three CPUID definitions placed fields on top of others, contrary to the
SDM:

- CPUID.7.1:ECX[1] RDT_A_ASYM was defined at bit 0.
- CPUID.7.1:EDX[17] UIRET_UIF was defined at bit 15.
- CPUID.23H.4:EBX was defined twice, with CNTR at bits 7:0 instead of
  7:4 and XER at bits 23:17 instead of 23:16.

All of these fields are forced to 0, so generated profiles give guests
the same values as before.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Overlapping definitions let one definition silently override another.
Overlapping bit ranges within a register left the uncovered bits
zeroed. Two entries for the same CPUID register and sub-leaf, or for
the same MSR, produce duplicate profile data, and only the first entry
is applied. Check when the tables are built that the bit ranges of a
register are disjoint and fit into it, that no two entries cover the
same CPUID register and sub-leaf, and that no MSR is defined twice.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Correct three CPUID definitions against SDM 325462-093:

- CPUID.4:ECX is the 32-bit number of cache sets, but bit 31 was not
  covered and therefore zeroed.
- CPUID.18H.0:EBX/ECX/EDX are reserved. Only sub-leaves 1 and above
  describe TLBs, so the TLB fields no longer apply to sub-leaf 0.
- CPUID.AH:EBX[12:8] and EDX[19:16] were missing. They stay 0 like the
  rest of the hidden PMU, but are now documented.

Generated profiles now match the SDM. Guests see no difference under
KVM: KVM reports 0 for leaf 0x18, and CPUID.4:ECX[31] is 0 on real
hardware.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
The profiles hide RDT allocation, but passed the host's CPUID.10H.1/2
capacity bitmask length and bitmap through. They also inherited the
XSAVE sub-leaves 19 to 63, whose XCR0 bits the SDM reserves. A profile
generated on an APX host would thus describe XSAVE component 19 without
advertising it in XCR0. Zero both, consistent with the feature bits.
Under KVM, existing profiles already resolve to 0 for these fields.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
XSAVE sub-leaf 13 describes the HDC state component, not CET. Bits 9
and 10 of CPUID.FH.1:EAX shared one short name. Also fix two typos.
The profiles do not change.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
The profiles hide protection keys (CPUID.7.0:ECX PKU and OSPKE), but
still advertised the PKRU state component in CPUID.DH.0:EAX[9] and its
size and offset in sub-leaf 9. Linux guests notice the mismatch and
drop PKRU, but other guests could enable it in XCR0, because KVM only
derives the allowed XCR0 bits from leaf 0xD. Hide the component too in
profiles generated from now on.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Profiles forced CPUID.1:ECX[5] (VMX) to 1. KVM only reports VMX when
kvm_intel's nested parameter is enabled, and the profile is checked
against the host before CHV clears VMX for VMs without nested
virtualization. On hosts with nested=0, every VM using such a profile
fails to start, even with nested=off.

Take the bit from the host in profiles generated from now on. CHV
still clears it for VMs without nested virtualization, and migrations
still require it on the destination if the source has it.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
CPUID.7.0:EBX[6] (FDP_EXCPTN_ONLY) and [13] (ZERO_FCS_FDS) came from
the host, so the guest CPUID of a profile differed between hosts. The
hardware decides about the x87 behavior either way, and the KVM docs
recommend to always set both bits in guest CPUID. Do so in profiles
generated from now on, which makes them identical on all hosts.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Profiles hide WAITPKG, but still allowed the "enable user wait and
pause" control in IA32_VMX_PROCBASED_CTLS2, as the Sapphire Rapids
profile does. KVM clears that control when KVM_SET_CPUID2 hides
WAITPKG, but CHV writes the profile's VMX MSRs afterwards and so
re-enables it. With nested virtualization, an L1 hypervisor can then
let L2 run UMWAIT and TPAUSE.

Hide the control. Do the same for "enable ENCLS exiting", as SGX is
hidden too: a profile generated on a host with SGX would otherwise make
KVM reject VMs with nested virtualization on every host without SGX.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Two VMX capability values describe the KVM version rather than the
CPU, yet profiles inherited them from the generation host:

- IA32_VMX_VMCS_ENUM is computed from the VMCS fields that KVM
  emulates, and KVM accepts any value on writes. A profile generated
  with a newer KVM could thus announce VMCS fields that the running
  KVM does not implement. Take it from the host instead.
- IA32_VMX_BASIC[58] (VMX nested-exception support, for FRED) was not
  defined and thus inherited. Once KVM reports it, a profile generated
  with that KVM would be rejected by older KVM versions. Hide it, as
  FRED is not exposed.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
- IA32_BARRIER is enumerated by CPUID.7.1:EAX[27], but its compile
  time check looked at sub-leaf 0, where EAX holds the maximum
  sub-leaf. The check thus verified nothing.
- The IA32_MCU_CONTROL range ended at decimal 1406 instead of 0x1406,
  so it was empty.
- IA32_ARCH_CAPABILITIES[62] (ITS_NO) is set if the system is not
  affected, as Linux and KVM use it. The description copied the
  inverted wording of SDM 325462-093.
- IA32_MCG_CAP is not zeroed for CPU profiles: CHV does not configure
  machine checks, so KVM reports its default.

The profiles do not change.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
TL;DR: v2 profiles carry the definition fixes, v1 stays as it was.

The existing profiles cannot change in place: CHV checks a migrated or
restored VM's CPUID against the profile on the destination, so e.g.
hiding PKRU would refuse every VM that was started with the old data.
Add sapphire-rapids-v2 and cascade-lake-v2 for new VMs instead. Compared
to v1, they:

- take the VMX bit from the host, so they work with kvm_intel nested=0,
- hide the PKRU XSAVE component, as PKU is hidden,
- always set the x87 defeature bits CPUID.7.0:EBX[6] and [13],
- hide the "enable user wait and pause" VMX control (Sapphire Rapids),
- take IA32_VMX_VMCS_ENUM from the host,
- match the SDM for CPUID.4:ECX[31], leaf 0x18 sub-leaf 0, the RDT
  sub-leaves, and the XSAVE sub-leaves 19 and above. Guests under KVM
  see no difference there.

The original generation hosts were not available. Both profiles were
re-derived from the v1 data: the generation host's KVM values were
reconstructed from it, and the generator then ran with the current
definitions. With the previous definitions, the same procedure
reproduces v1 exactly.

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
@phip1611
phip1611 force-pushed the grdnlnx-cpu-profile-fixes branch from 3b97396 to baf412c Compare November 30, 2026 15:52
@olivereanderson

olivereanderson commented Dec 1, 2026 •

Copy link
Copy Markdown

Snapshots and live migrations now save every MSR the host's KVM lists. Before, only the MSRs known when the profile was generated were saved, so guests silently lost IA32_DEBUGCTL (bus-lock detection) on hosts with Linux 7.1 or newer and MISC_FEATURES_ENABLES (CPUID faulting) everywhere.

This is intentional behavior: Only MSRs that are compatible with the profile should be snapshotted/transferred, because otherwise they are not compatible with the profile/lowest common denominator in your fleet anyway and setting them will fail.

An earlier version on our fork even made it impossible to access such MSRs, but the MSR filter logic was complex and not desired upstream.

IA32_ARCH_CAPABILITIES bit 63 is treated as reserved.

I know it is reserved in the SDM, but I actually found it documented here: https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html

The Cascade Lake profile is accepted on newer hosts booted with tsx=off

I think that this was already the case.

CPUID compatibility check (also for VMs without a profile):

The AVX10 version is checked.
The x87 defeature bits are ignored.

This is on my radar and needs to be two separate PRs.

takes the VMX bit from the host, so it works with kvm_intel nested=0

It already works when nested=off. It is however more consistent/technically correct to use passthrough here (already done in our upstreaming branch(es)).

always sets the x87 defeature bits,

Then we cannot migrate to older instances whose compatibility checks do not ignore those de-feature bits. If it is policy never to migrate to older, then that is fine, but it would make more sense for the VMM to set this and not CPU profiles.

takes VMCS_ENUM from the running host,

This does not sound correct to me.

the definition tables are corrected against the SDM, a compile-time check rejects overlapping or duplicate definitions

It is good to check this, but I realized that checking these large tables at compile time can make the IDE sluggish when working in that file. We do this as a unit test in the upstreaming branch(es).

New profiles sapphire-rapids-v2 and cascade-lake-v2.

Were these generated on real deployments?

I think we should not introduce new profiles now, unless critical, and rather re-generate profiles once the upstreaming work is completed. We should then see if we can just replace the existing profiles directly (provided that the impact is not too severe), because we want to maintain as few profiles as possible.

The other points in the overview seem reasonable, but I would have to think about them a bit more to be sure.

@phip1611

phip1611 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@olivereanderson thanks! Could you please also write down the fixes that are good and we should keep?

@phip1611
phip1611 marked this pull request as draft December 1, 2026 08:58
@olivereanderson

Copy link
Copy Markdown

Could you investigate this one a bit more:
arch: Accept FB_CLEAR profiles on hosts with TSX disabled (f2cd7bb)
and upstream the change if it makes sesne.

These two can also be upstreamed pretty much right away:

  • arch: Check the AVX10 version for CPUID compatibility (7ddf490)
  • arch: Ignore x87 defeature bits in CPUID compatibility (43b0ba9)

I think we should ignore the rest and rather focus on upstreaming the CPU profile generation tool itself as that effort addresses most of the other points anyway.

@phip1611 phip1611 closed this Dec 1, 2026
@phip1611
phip1611 deleted the grdnlnx-cpu-profile-fixes branch December 1, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants